//
//  SY_Activity.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 16/9/7.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "SY_Activity.h"
#import "MBProgressHUD.h"

#import "SplashScreenView.h"
#import "SYGuideViewController.h"
#import "WebViewController.h"
#import "SY_Home_TeaLeavingViewController.h"
#import "HomeParLeaveListController.h"
#import "HomeParLeaveDetailController.h"
#import "FR_ManagerInfoViewController.h"
#import "FR_VisitortViewController.h"
#import "FR_History_Identity_ViewController.h"
#import "FR_HistoryViewController.h"
#import "FR_StudentRecordList_ViewController.h"
#import "SYHomeParLeaveViewController.h"
#import "FR_AccountViewController.h"
//#import "FR_StudentAccountViewController.h"
#import "FR_TeacherAcountViewController.h"
#import "FR_MyAcountViewController.h"
#import "FR_Par_HistoryViewController.h"
#import "Persnoal_Information_ViewController.h"
#import "ChangeRoleViewController.h"
#import "AboutAppViewController.h"
#import "SY_Setting_PassWordViewController.h"
#import "SY_Setting_MakeSurePhoneNumViewController.h"
#import "FR_Record_InformationViewController.h"
#import "FR_LateViewController.h"    //迟到
#import "FR_Attendance_ViewController.h" //考勤
#import "FR_AbsentClass_ViewController.h" //旷课
#import "FR_EnterAndLeaveMessageController.h"
#import "FR_Par_Attendance_ViewController.h" //家长
#import "SY_Par_AttendanceViewController.h"


#import "SYNotiViewController.h"
#import "PushNotiViewController.h"
#import "SYHomeworkViewController.h"
#import "DetailHvkViewController.h"
#import "AssignHomeWorkViewController.h"

@interface SY_Activity ()

@property(nonatomic,strong) NSMutableArray *classeids;

@end


@implementation SY_Activity 
{
    
    MainViewController *_mainVC;
    
    UIViewController *_currentVC;
    
    NSInteger _activeOrder;
    
    MBProgressHUD *hud;
    
    SY_Activity_Model *activityModel;
    
    UIViewController *moveToVC;
}


static SY_Activity *activity = nil;

+ (instancetype)shareActivity {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        activity = [[SY_Activity alloc] init];
    });
    
    return activity;
}

- (id)init
{
    self = [super init];
    if (self) {
        _activeOrder = 0;
    }
    return self;
}


-(void)action {
    
    SY_Activity_Model *model = [self.instructionArr objectAtIndex:_activeOrder];
    activityModel = model;
    
    switch (model.apptype) {
        case 0:
            _activityType = SYActivityTypeText;
            break;
        case 1:
            _activityType = SYActivityTypeBanner;
            break;
        case 2:
            _activityType = SYActivityTypeInstruction;
            break;
        case 3:
            _activityType = SYActivityTypeWeb;
            break;
            
        default:
            break;
    }
    
    if (_activityType == SYActivityTypeText) {
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        
        hud.delegate = self;
        hud.mode = MBProgressHUDModeText;
        //hud.detailsLabelText = model.scontent;
        hud.detailsLabel.text = model.scontent;
        //hud.detailsLabelFont = [UIFont systemFontOfSize:17];
        hud.detailsLabel.font = [UIFont systemFontOfSize:17];
        hud.margin = 10.f;
        //hud.yOffset = 150;
        [hud setOffset:CGPointMake(hud.offset.x, 150)];
        hud.removeFromSuperViewOnHide = YES;
        
        //[hud showAnimated:YES whileExecutingBlock:nil];
        [hud showAnimated:YES];
        //[hud hide:YES afterDelay:model.showtime];
        [hud hideAnimated:YES afterDelay:model.showtime];
        
        
    }else if (_activityType == SYActivityTypeBanner) {
        
        [model.fileid componentsSeparatedByString:@","];
        
        SYGuideViewController *guide=[[SYGuideViewController alloc]init];
        guide.webImageNameArr = [model.fileid componentsSeparatedByString:@","];
        guide.isSkip = model.isskip;
        guide.viewWasHide = ^(){
            if (model.moveto) {
                
                UIViewController * targetVC = [self getMoveTotargetWithNum:model.moveto];
                
                if (targetVC) {
                    if (model.moveto == 2102) {
                        
                        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:targetVC];
                        
                        [self->_currentVC presentViewController:nav animated:YES completion:nil];
                        
                    }else {
                        [self->_currentVC.navigationController pushViewController:targetVC animated:YES];
                    }
                    
                }else {
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"请切换角色以使用此功能" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alertView show];
                }
            }else {
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"当前版本暂不支持此功能" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alertView show];
            }
            [self nextAction];
        };
        _currentVC = [Utils getCurrentVC];
        [_currentVC.navigationController presentViewController:guide animated:YES completion:nil];
        
        
    }else if (_activityType == SYActivityTypeInstruction){
        
        _currentVC = [Utils getCurrentVC];
        
        if (model.motion) {
            SYGuideViewController *guide=[[SYGuideViewController alloc]init];
            guide.guideType = [model.motion integerValue];
            guide.isSkip = model.isskip;
            guide.viewWasHide = ^(){
                
                if (model.moveto) {
                    
                    if (model.moveto == 2001) {
                        NSURL *cleanURL = [NSURL URLWithString:model.url];
                        [[UIApplication sharedApplication] openURL:cleanURL];
                        
                        return;
                    }
                    UIViewController * targetVC = [self getMoveTotargetWithNum:model.moveto];
                    
                    if (targetVC) {
                        if (model.moveto == 2102) {
                            
                            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:targetVC];
                            
                            [self->_currentVC presentViewController:nav animated:YES completion:nil];
                            
                        }else {
                            [self->_currentVC.navigationController pushViewController:targetVC animated:YES];
                        }
                        
                    }else {
                        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"请切换角色以使用此功能" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                        [alertView show];
                    }
                }else {
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"当前版本不支持此功能" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alertView show];
                }
                [self nextAction];
            };
            
            [_currentVC.navigationController presentViewController:guide animated:YES completion:nil];
            
        }else {
            
            _currentVC = [Utils getCurrentVC];
            
            if (model.moveto) {
                
                if (model.moveto == 2001) {
                    NSURL *cleanURL = [NSURL URLWithString:model.url];
                    [[UIApplication sharedApplication] openURL:cleanURL];
                    
                    return;
                }
                UIViewController * targetVC = [self getMoveTotargetWithNum:model.moveto];
                
                if (targetVC) {
                    if (model.moveto == 2102) {
                        
                        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:targetVC];
                        
                        [self->_currentVC presentViewController:nav animated:YES completion:nil];
                        
                    }else {
                        [self->_currentVC.navigationController pushViewController:targetVC animated:YES];
                    }
                }else {
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"请切换角色以使用此功能" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alertView show];
                }
            }else {
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"当前版本不支持此功能" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alertView show];
            }
           
           
            [self nextAction];
        }
        
        
        
    }
    
}

-(void)hudWasHidden:(MBProgressHUD *)hud {
    
    NSLog(@"隐藏了");
    [self nextAction];
    
}

#pragma mark - 执行完之后判断后面还有别的任务没有

- (void)nextAction{
    
    _activeOrder += 1;
    if (_activeOrder < [self.instructionArr count]) {
        [self action];
    }else {
        _activeOrder = 0;
    }
}


- (UIViewController *)getMoveTotargetWithNum:(NSInteger )num {
    
    moveToVC = nil;
    
    if ([SYAccountTool ShareAccount].account.lasttype == 1 && num > 4020 && num < 4031) {

        return moveToVC;
    }

    if ([SYAccountTool ShareAccount].account.lasttype == 2 && num > 4000 && num < 4011) {

        return moveToVC;
    }
   
    if (num == 2001) {
        
        NSString *textURL = @"http://www.baidu.com/";
        NSURL *cleanURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", textURL]];
        [[UIApplication sharedApplication] openURL:cleanURL];
        
        
    }else if (num == 2002) {
        WebViewController *targetVC = [[WebViewController alloc]init];
        targetVC.requestURL = activityModel.url;
        
        moveToVC = targetVC;
    }else if (num == 4001) {
        
        if ([SYAccountTool ShareAccount].account.lasttype == 1 ) {
            SY_Home_TeaLeavingViewController *targetVC=[[SY_Home_TeaLeavingViewController alloc]init];
            moveToVC = targetVC;
            
        }else {
            moveToVC = nil;
        }
        
        
    }else if (num == 24) {
       
        HomeParLeaveDetailController *targetVC = [[HomeParLeaveDetailController alloc]init];
        targetVC.leaveid = [activityModel.infoid integerValue];
        targetVC.msgID = activityModel.msgid;
        moveToVC = targetVC;
       
       
    }else if (num == 4003) {//考勤
        
        FR_Attendance_ViewController * targetVC =[[FR_Attendance_ViewController alloc]init];
        
        moveToVC = targetVC;
        
        
    }else if (num == 4004) {//底片
        FR_ManagerInfoViewController *targetVC = [[FR_ManagerInfoViewController alloc] init];
        moveToVC = targetVC;
    }else if (num == 4005) {//访客列表
        FR_VisitortViewController *targetVC =[[FR_VisitortViewController alloc]init];
        targetVC.visitId = activityModel.infoid;
        moveToVC = targetVC;
    }else if (num == 4006) {//统计
        FR_AccountViewController *targetVC=[[FR_AccountViewController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 4007) {//班级统计
        FR_TeacherAcountViewController * targetVC = [[FR_TeacherAcountViewController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 4008) {//个人统计
        FR_MyAcountViewController * targetVC =[[FR_MyAcountViewController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 4009) {//迟到
        FR_LateViewController * targetVC =[[FR_LateViewController alloc]init];
        
        moveToVC = targetVC;
        
        
    }else if (num == 4010) {//旷课
        FR_AbsentClass_ViewController * targetVC =[[FR_AbsentClass_ViewController alloc]init];
        
        moveToVC = targetVC;
        
        
    }else if (num == 4021) {//请假列表家长
        HomeParLeaveListController *targetVC=[[HomeParLeaveListController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 4022) {//请假详情
        HomeParLeaveDetailController *targetVC = [[HomeParLeaveDetailController alloc]init];
        targetVC.leaveid = [activityModel.infoid integerValue];
        moveToVC = targetVC;
    }else if (num == 4023) {//发起请假
        if ([SYAccountTool ShareAccount].account.lasttype != 2) {
            return nil;
        }
        SYHomeParLeaveViewController *targetVC = [[SYHomeParLeaveViewController alloc] init];
        
        moveToVC = targetVC;
    }else if (num == 4024) {//底片列表
        FR_ManagerInfoViewController *targetVC = [[FR_ManagerInfoViewController alloc] init];
        moveToVC = targetVC;
    }else if (num == 4025) {//底片管理
        FR_HistoryViewController *targetVC = [[FR_HistoryViewController alloc]init];
        
        moveToVC = targetVC;
    }
//    else if (num == 4026) {//统计
//
//        FR_StudentAccountViewController *targetVC=[[FR_StudentAccountViewController alloc]init];
//        moveToVC = targetVC;
//    }
    else if (num == 4027) {//记录
        FR_Par_HistoryViewController *targetVC=[[FR_Par_HistoryViewController alloc]init];

        moveToVC = targetVC;
    }else if (num == 4028) {//记录详情
        
        FR_EnterAndLeaveMessageController *targetVC = [[FR_EnterAndLeaveMessageController alloc] init];
        targetVC.infoID = activityModel.infoid;
        targetVC.classID = activityModel.classid;
        targetVC.sciD = activityModel.scid;
        targetVC.description2 = activityModel.description2;
//        FR_Record_InformationViewController *targetVC = [[FR_Record_InformationViewController alloc]init];
//        targetVC.classID = activityModel.classid;
//        targetVC.infoID = activityModel.eventid;
//        targetVC.schoolID = @"0";
//        targetVC.recordType = 0;
        moveToVC = targetVC;
        
    }else if (num == 4029) {//迟到
        
        FR_Par_Attendance_ViewController *targetVC = [[FR_Par_Attendance_ViewController alloc]init];
        targetVC.arrival = 2;
        
        moveToVC = targetVC;
        
    }else if (num == 4030) {//旷课
        FR_Par_Attendance_ViewController *targetVC = [[FR_Par_Attendance_ViewController alloc]init];
        targetVC.arrival = 0;
        
        moveToVC = targetVC;
        
    }else if (num == 4041) {//个人信息
        Persnoal_Information_ViewController *targetVC = [[Persnoal_Information_ViewController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 4042) {//修改密码
        SY_Setting_MakeSurePhoneNumViewController *targetVC = [[SY_Setting_MakeSurePhoneNumViewController alloc]init];
        targetVC.typeNum = @"1";
        
        moveToVC = targetVC;
    }else if (num == 4043) {//更改手机号
        SY_Setting_PassWordViewController *targetVC =[[SY_Setting_PassWordViewController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 4044) {//问题反馈
        
        
    }else if (num == 4045) {//关于
        AboutAppViewController *targetVC = [[AboutAppViewController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 4046) {//角色切换
        ChangeRoleViewController *targetVC = [[ChangeRoleViewController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 201) {//班级通知
       
       SYNotiViewController *targetVC = [[SYNotiViewController alloc] init];
       moveToVC = targetVC;
    }else if (num == 202) {//我发出的
       
       SYNotiViewController *targetVC = [[SYNotiViewController alloc] init];
       moveToVC = targetVC;
    }else if (num == 203) {//家长
       if ([SYAccountTool ShareAccount].account.lasttype != 2) {
          return nil;
       }
       SYNotiViewController *targetVC = [[SYNotiViewController alloc] init];
       moveToVC = targetVC;
    }else if (num == 204) {//发通知
       if ([SYAccountTool ShareAccount].account.lasttype != 1) {
          return nil;
       }
       PushNotiViewController *targetVC = [[PushNotiViewController alloc] init];
       moveToVC = targetVC;
    }else if (num == 2 || num == 3) {//通知详情
       
       DetailHvkViewController *targetVC = [[DetailHvkViewController alloc] init];
       targetVC.infoID = activityModel.infoid;
       targetVC.msgID = activityModel.msgid;
       
       moveToVC = targetVC;
       
    }else if (num == 301) {//班级作业
       
       SYHomeworkViewController *targetVC = [[SYHomeworkViewController alloc] init];
       moveToVC = targetVC;
    }else if (num == 302) {//我发出的
       
       SYHomeworkViewController *targetVC = [[SYHomeworkViewController alloc] init];
       moveToVC = targetVC;
    }else if (num == 303) {//家长
       if ([SYAccountTool ShareAccount].account.lasttype != 2) {
          return nil;
       }
       SYHomeworkViewController *targetVC = [[SYHomeworkViewController alloc] init];
       moveToVC = targetVC;
    }else if (num == 304) {//发作业
       if ([SYAccountTool ShareAccount].account.lasttype != 1) {
          return nil;
       }
       AssignHomeWorkViewController *targetVC = [[AssignHomeWorkViewController alloc] init];
       moveToVC = targetVC;
    }else if (num == 14 || num == 15){
       
       SY_Par_AttendanceViewController *targetVC = [[SY_Par_AttendanceViewController alloc]init];
       targetVC.infoID = activityModel.infoid;
       targetVC.magID = activityModel.msgid;
       moveToVC = targetVC;
    }

    return moveToVC;
}

@end
